<?php
namespace app\common\entity;

use think\Db;
use think\Model;

class Goods extends Model
{
    /**
     * @var string 对应的数据表名
     */
    protected $table = 'goods';

    public static function getTypes()
    {
        return [
            1 => '一次性',
            2 => '每天',
        ];
    }

    public static function getOneTypes($types)
    {
        switch ($types) {
            case 1:
                return '一次性';
            case 2:
                return '每天';
            default:
                return '';
        }
    }

    public static function getAllCate()
    {

        return [
            '1' => '公告',
            '2' => '新手解答',
//            '3' => '便民'
        ];
    }
    #搜索商品
    public function hckGoodsLists($category,$keyword,$page,$limit)
    {
        return $this->where('goods_title','like',"%".$keyword."%")->where('category_id',$category)->where('status',1)->order('sort')->select();
    }
    /*下拉加载商品*/
    public function hcGoodsLists($category,$page,$limit)
    {
        return $this->where('category_id',$category)->where('status',1)->order('sort')->select();
    }
    public function getGoodsLists($page,$limit)
    {
        return $this->field('id,category_id,goods_title,goods_describe,goods_prices,goods_pic')->where('status',1)->order('sort')->select();
    }
    public function hkGoodsLists($keyword,$page,$limit)
    {
        
        return $this->where('goods_title','like',"%".$keyword."%")->where('status',1)->order('sort')->select();
    }
    /*
     * 分页商品列表
     * */
    public function getGoodsPages($category_id){

        if($category_id){
            $pageParam['query']['type'] = $category_id;         //type 分类参数
            $data = $this->where('category_id',$category_id)->paginate(10,false,$pageParam);
        }else{
            $data1 = $this->select();
            $count = count($data1);
            $data = $this->paginate(10,$count);
        }

        return $data;
    }

    public function getGoodsDetail($goodsId){
        return $this->where(array('id'=>$goodsId))->find();
    }

    public function addGoods($data)
    {
        $entity = new self();
        $entity->category_id = $data['category'];
        $entity->goods_title = $data['title'];
        if (!empty($data['content'])){

            $entity->goods_content = $data['content'];
        }
        $entity->goods_describe = $data['goods_describe'];
        $entity->goods_prices = $data['prices'];
        $entity->goods_num = $data['num'];
        $entity->goods_pic = json_encode($data['photo']);
        $entity->status = $data['status'];
        $entity->addtime = time();
        $entity->sort = $data['sort'] ?? 0;
        $entity->is_index = $data['is_index'] ?? 1;
        $entity->is_pp = $data['is_pp'] ?? 0;


        return $entity->save();
    }

    public function updateGoods(Goods $goods, $data)
    {
        $goods->category_id = $data['category'];
        $goods->goods_title = $data['title'];
        if (!empty($data['content'])){

            $goods->goods_content = $data['content'];
        }

        $goods->goods_describe = $data['goods_describe'];
        $goods->goods_prices = $data['prices'];
        $goods->goods_num = $data['num'];
        $goods->goods_pic = json_encode($data['photo']);
        $goods->status = $data['status'];
        $goods->sort = $data['sort'] ?? 0;
        $goods->is_index = $data['is_index'];
        $goods->is_pp = $data['is_pp'] ?? 0;


        return $goods->save();
    }

    /*
     * @param 商品ID
     * 根据商品ID 查询商品图
     * */
    public function getGoodsPic($id){
        $goods = new self();
        $goodsList = $goods->where(array('id'=>$id))->find();
        $goodsList['goods_content'] = htmlspecialchars_decode($goodsList['goods_content']);
        //提取商品内容中的图片
        $pattern='/<img((?!src).)*src[\s]*=[\s]*[\'"](?<src>[^\'"]*)[\'"]/i';
        preg_match_all($pattern,$goodsList['goods_content'],$out);
        return $out[2];
    }

    /*
     * 添加商品分类
     * */
    public function addGoodsCategoryParent($data){
        $entity = DB::table("goods_category");
        $entity->category_title = $data['category_title'];
        return DB::table("goods_category")->save();
    }

    /*
     * 添加商品分类
     * */
    public function addGoodsCategorySon($data){
        $entity = new self();
        $entity->category_title = $data['category_title'];
        return $entity->save();
    }




}